/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.ball.controllers
{
	import com.puny.ball.utils.events.ShotEvent;
	import com.puny.ball.views.ShotView;
	import com.puny.mvc.controllers.Controller;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	
	public class ShotController extends Controller
	{
		private const MAX_LENGTH:Number = 100;
		private const MIN_LENGTH:Number = 5;
		private const _pt:Point = new Point();
		private const _power:Point = new Point();
		private const _guage:Point = new Point();
		private const _impact:ShotEvent = new ShotEvent(ShotEvent.IMPACT);
		private var _offset:DisplayObject;
		private var _view:ShotView;
		private var _enable:Boolean = false;
		private var _active:Boolean = false;
		private var _power_ratio:Number = .25;
		public function ShotController(value:ShotView=null)
		{
			_view = value;
			super();
		}
		
		/**
		 * 
		 * @param offset
		 * 
		 */
		public function set mapView(offset:DisplayObject):void {
			_offset = offset;
		}
		
		/**
		 * .25 in default 
		 * @param value
		 * 
		 */
		public function set powerRatio(value:Number):void {
			_power_ratio = value;
		}
		
		protected function set view(value:ShotView):void {
			_view = value;
		}
		
		protected function get view():ShotView {
			return _view;
		}
		
		/**
		 * 
		 * @param p
		 * 
		 */
		public function activateAt(p:Point):void {
			_pt.x = p.x + _offset.x;
			_pt.y = p.y + _offset.y;
			_view.x = _pt.x;
			_view.y = _pt.y;
//			_view.visible = true;
			_view.alpha = 1;
			active = true;
		}
		
		/**
		 * 
		 * @param value
		 * 
		 */
		public function set enable(value:Boolean):void {
			_enable = value;
			activateDownHandler(_enable&&_active);
		}
	
		override protected function setup():void {
			if (_view == null) {
				_view = new ShotView(null);
			}
			addChild(_view);
			_view.rotation = 180;
		}
		
		override public function beforeReady():void {
			super.beforeReady();
			enable = true;
			addChild(_view);
		}
		
		override protected function beforeChildrenRemoved():void {
			enable = false;
			_offset = null;
//			_view.visible = false;
		}
		
		private function set active(value:Boolean):void {
			_active = value;
			activateDownHandler(_enable&&_active);
		}
		
		private function activateDownHandler(value:Boolean):void {
			if (stage != null) {
				if (value) {
					stage.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				} else {
					stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				}
			}
		}

		private function downHandler(event:Event):void {
			var dx:Number = mouseX - _pt.x;
			var dy:Number = mouseY - _pt.y;
			//			Mouse.hide();
			if (dx * dx + dy * dy < 10000) {
				if (!stage.hasEventListener(MouseEvent.MOUSE_MOVE)) {
					active = false;
					//					stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				}
				//				stage.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
				drawLine();
			}
		}
		
		private function moveHandler(event:Event):void {
			drawLine();
		}
		
		private function drawLine():void {
			var rad:Number;
			var length:Number;
			_power.x = mouseX - _pt.x;
			_power.y = mouseY - _pt.y;
			length = _power.length;
			if (length > 0) {
				_guage.x = _power.x * MAX_LENGTH / length;
				_guage.y = _power.y * MAX_LENGTH / length;
				if (length > MAX_LENGTH) {
					_power.x = _guage.x;
					_power.y = _guage.y;
				}
				rad = Math.acos(_power.x / _power.length);
				if (_power.y < 0) {
					rad = 2 * Math.PI - rad;
				}
				
				_view.rotation = rad * 180 / Math.PI+90;
				_view.power = _power.length / MAX_LENGTH;
				
			}
		}
		
		private function upHandler(event:Event):void {
			var length:Number = _power.length;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
			
			if (_power.length < MIN_LENGTH) {
				_power.x = _power.x * MIN_LENGTH / length;
				_power.y = _power.y * MIN_LENGTH / length;
			}
			_pt.x = -_power.x * _power_ratio;
			_pt.y = -_power.y * _power_ratio;
			trace("power=", _power);
			_view.release();
			//			Mouse.show();
			_impact.power = _pt;
			dispatchEvent(_impact.clone());
		}
	}
}